package domain;

import common.Constants;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.OneToMany;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import org.hibernate.annotations.NamedQueries;
import org.hibernate.annotations.NamedQuery;
import org.hibernate.validator.Email;
import org.hibernate.validator.NotNull;
import org.hibernate.validator.Past;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotEmpty;

/**
 * Object for representing user
 * 
 * @author yuyok
 */
@Entity(name = "User")
@NamedQueries({
    @NamedQuery(name = "getUserByLoginAndPassword",
    query = "SELECT u FROM User u WHERE u.login = :login AND u.password = :password"),
    @NamedQuery(name = "getUserByLogin",
    query = "SELECT u FROM User u WHERE u.login = :login"),
    @NamedQuery(name = "getUsersByRole",
    query = "SELECT u FROM User u WHERE u.role = :role"),
    @NamedQuery(name = "getCommentsByUserLogin",
    query = "SELECT u.comments FROM User u WHERE u.login = :login"),
    @NamedQuery(name = "getAllUsers",
    query = "SELECT u FROM User u")
})
public class User extends DomainObject {

    private static final long serialVersionUID = 1L;
    @NotEmpty(message = "please, fill in your first name")
    private String firstName;
    @NotEmpty(message = "please, fill in your last name")
    private String lastName;
    @NotEmpty(message = "please, fill in your login")
    @Column(unique = true)
    private String login;
    @NotEmpty(message = "please, fill in your password")
    @Length(min = 6, max = 10, message = "please, input from 6 to 10 digits")
    private String password;
    @NotNull
    @Enumerated
    @Column(name = "userRole")
    private String role = Constants.USER_ROLE;
    @NotEmpty
    @Column(unique = true)
    @Email
    private String email;
    @Temporal(TemporalType.DATE)
    @Past
    private Date birthDate;
    @Embedded
    private AccountInfo accountInfo;
    @NotNull
    private boolean isEnable = false;
    @OneToMany(mappedBy = "user",  
    cascade = {CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH}) 
    private List<Comment> comments = new ArrayList<Comment>();
    @OneToMany(mappedBy = "user", 
    cascade = {CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH}) 
    private List<Video> videos = new ArrayList<Video>();

    public User() {
    }

    public List<Comment> getComments() {
        return this.comments;
    }

    public void addComment(Comment comment) {
        this.comments.add(comment);
    }

    public List<Video> getVideos() {
        return this.videos;
    }

    public void addVideo(Video video) {
        this.videos.add(video);
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getFirstName() {
        return this.firstName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getLastName() {
        return this.lastName;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getEmail() {
        return email;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    public String getLogin() {
        return login;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getPassword() {
        return password;
    }

    public void setRole(String role) {
        this.role = role;
    }

    public String getRole() {
        return role;
    }

    public void setBirthDate(Date birthDate) {
        this.birthDate = birthDate;
    }

    public Date getBirthDate() {
        return birthDate;
    }

    public AccountInfo getAccountInfo() {
        return accountInfo;
    }

    public void setAccountInfo(AccountInfo accountInfo) {
        this.accountInfo = accountInfo;
    }

    public boolean isEnable() {
        return isEnable;
    }

    public void setIsEnable(boolean isEnable) {
        this.isEnable = isEnable;
    }
}
